import 'package:flutter/material.dart';
import 'record.dart';
import 'recordDetail.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'Util.dart';
import 'dart:io';
import 'package:dio/adapter.dart';
import 'package:dio/dio.dart';
class CardDetailPage extends StatefulWidget {
  CardDetailPage({Key key, this.title, this.para,this.cardList}) : super(key: key);
  final String title;
  final Map<String, dynamic> para;
  final List<Map<String, dynamic>> cardList;
  @override
  _CardDetailPage createState() => _CardDetailPage();
}

class _CardDetailPage extends State<CardDetailPage> {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }
  checkNewCustomer(){
    SharedPreferences.getInstance().then((value){
      setState(() {
        String domain = value.getString("buildings").toMap()["subdomain"].toString();
        String block_key = value.getString("buildings").toMap()["block_key"].toString();
        print(domain);
        Map<String, dynamic> dic = {};
        dic["api_key"] = "xQITe6bZFUrmeJbkcKgkUzajUI6FGLA";
        dic["lang"] = "en";
        dic["block_key"] = block_key;
        print(dic);
        var dio = Dio();
        if (ShareHandle.instance.proxy.length > 0){
          (dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate = (HttpClient client){
            client.findProxy = (uri) {
              return ShareHandle.instance.proxy;
            };
          };
        }
        dio.post("${domain}/api/visitor/new_record",data:FormData.fromMap(dic)).then((response){
          final result = response.toResult();
          print(result.item1);
          if (result.item2){
            setState(() {
              // this.dataList = List<Map<String, dynamic>>.from(result.item1["container"] ?? []);
              var dic = widget.para;
              Navigator.push( context, MaterialPageRoute(builder: (context) => RecordDetailPage(title: widget.title,para: dic,customer: result.item1["container"],)),);
            });
          }else{
            showAlert(result.item4);
          }
        }).catchError((error){
          print("error:${error}");
          showAlert(error.toString());
        });
      });
    });
  }
  @override
  Widget build(BuildContext context) {
    List<Container> cards = [];
    for (var v in this.widget.cardList) {
      Container card = Container(
        //width: 335,
        //height: 368,
        margin: EdgeInsets.only(left: 35, right: 35, top: 10, bottom: 18),
        decoration: BoxDecoration(
          border: Border.all(
            color: Colors.grey, //边框颜色
            width: 1, //边框宽度
          ), // 边色与边宽度
          color: Colors.white, // 底色
          boxShadow: [
            BoxShadow(
              blurRadius: 2, //阴影范围
              spreadRadius: 1, //阴影浓度
              color: Colors.grey, //阴影颜色
            ),
          ],
          borderRadius: BorderRadius.circular(19),
        ),
        child: Stack(
          children: <Widget>[
            Image(
              fit: BoxFit.cover,
              image: AssetImage("assets/images/bg_password_detail.png"),
            ),
            Container(
              margin: EdgeInsets.only(
                  left: 30, top: 100, right: 30, bottom: 0),
//              color: Colors.amberAccent,
              child: Row(
                children: <Widget>[
                  Image(
                    image: AssetImage(
                        "assets/images/login_icon_visitor.png"),
                  ),
                  SizedBox(width: 8,),
                  Column(
                    children: <Widget>[
                      Text(
                        "當值職員：",
                        style: TextStyle(color: Colors.blue,),
                        textAlign: TextAlign.left,
                      ),
                      SizedBox(height: 10,),Text(v["staff_name"],textAlign: TextAlign.left,),SizedBox(height: 10,),Text("職員編號："),Text(v["staff_code"]),
                    ],
                  )
                ],
              ),
            )
          ],
        ),
      );
      cards.add(card);
    }
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
        actions: <Widget>[
          FlatButton(
            child: Text("登出",style: TextStyle(color: Colors.white),),
            onPressed: () {
              Navigator.of(context)
                  .popUntil((route) => route.settings.name == "MyHome2Page");
            },
          )
        ],
      ),
      body: Row(
        children: <Widget>[
          Expanded(
            child: ListView(children: cards,),
          ),
          Expanded(
              child: Container(
            color: Colors.white,
            child: Stack(
              children: <Widget>[
                Column(children: <Widget>[
                  Expanded(child: Center(
                    child: FlatButton(child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                        Image(
                          image:
                          AssetImage("assets/images/btn_check_new_vistor.png"),
                        ),
                        SizedBox(height: 5),
                        Text("核實新訪客"),
                      ],
                    ),onPressed: (){
                      checkNewCustomer();
                    },),
                  ),),
                  Expanded(child: Center(
                    child: FlatButton(child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                        Image(
                          image:
                          AssetImage("assets/images/btn_check_new_vistor.png"),
                        ),
                        SizedBox(height: 5),
                        Text("訪客記錄"),
                      ],
                    ),onPressed: (){
                      var dic = widget.para;
                      Navigator.push( context, MaterialPageRoute(builder: (context) => RecordPage(title: widget.title,para: dic)),);
                    },),
                  ),),
                ],)
              ],
            ),
          )),
        ],
      ),
    );
  }
}
